#!/data/data/com.termux/files/usr/bin/sh
LOGOPATH=`ls -l /dev/block/platform/*/*/by-name | grep logo | sed 's/.* //g'`
TMP=${TEMP_DIR}/firstboot
LOGO=${TMP}/logo.img
[[ -d ${TMP} ]] || mkdir -p ${TMP}

if [[ $1 == script ]]; then
	BUSYBOX="/data/adb/modules/shadow_screenshots/busybox"
	[[ -d ${BUSYBOX} ]] || BUSYBOX="/data/adb/lite_modules/shadow_screenshots/busybox"
	if [[ -d ${BUSYBOX} ]]; then
		export PATH=${BUSYBOX}/bin:${PATH}
		export LD_LIBRARY_PATH=${BUSYBOX}/lib:${LD_LIBRARY_PATH}
	else
		which convert > /dev/null
		if [[ $? -ne 0 ]]; then
			echo "未找到ImageMagick命令，建议安装阴影截图模块重启后重试"
			exit 1
		fi
	fi
	if [[ $2 == output ]]; then
		ohce()
		{
			for i in `seq ${#1} -2 0`
			do
				echo -n "${1:$i:2}"
			done
		}
		if [[ ! -f ${TOOLKIT}/xxd2 ]]; then
			curl -o ${TOOLKIT}/xxd2 -sL https://gitee.com/youngdriver/pio/raw/master/busybox/xxd
			chmod 777 ${TOOLKIT}/xxd2
			md5sum ${TOOLKIT}/xxd2 | grep '060f41432db4a641667948e99eadc32d' > /dev/null
			[[ $? -ne 0 ]] && echo "xxd下载失败" && exit 1
		fi
		TXT=${TMP}/logo16.txt
		dd if=${LOGOPATH} of=${LOGO} conv=notrunc
		xxd_c=16
		xxd2 -ps -c ${xxd_c} ${LOGO} > ${TXT}
		LINE=`sed -n '/^424d.\{8\}0\{8\}/=' ${TXT}`
		for i in ${LINE}
		do
			HEX=`sed -n "$i,$(($i + 1))p" ${TXT}`
			SKIP=$((${xxd_c} * ($i - 1))) #偏移值
			W=$((16#$(ohce ${HEX:37:8}))) #宽度
			L=$((16#$(ohce ${HEX:45:8}))) #高度
			COUNT=$(( $L * $W * 3 + 54 )) #体积
			dd if=${LOGO} skip=${SKIP} count=${COUNT} of=${TMP}/${i}.bmp bs=1 conv=notrunc
			if [[ $? -eq 0 ]]; then
				WH=$(identify ${TMP}/${i}.bmp 2>/dev/null | egrep -o ' [0-9]+x[0-9]+ ' | tr -d ' ' 2> /dev/null)
				W=${WH%%x*}
				H=${WH##*x}
				echo -e "SKIP=${SKIP}\nCOUNT=${COUNT}\nW=${W}\nH=${H}" > ${TMP}/${i}.xxd
				convert ${TMP}/${i}.bmp -gravity center -crop ${W}x${W}+0+0 -resize 100x100 ${TMP}/${i}.png 2>/dev/null
			fi
		done
	elif [[ $2 == input ]]; then
		source ${3%bmp}xxd
		SIZE=$(( ${W} * ${H} * 3 + 54 ))
		convert -resize ${W}x${H}! "${4}" ppm:- | convert - BMP3:${3}
		if [[ ${COUNT} -eq $(wc -c ${3} | cut -d ' ' -f 1) ]]; then
			echo "导入完成"
			BAKLOGO=${SDCARD_PATH}/logo-$(date +%F-%k-%M-%S).img
			cp -f ${LOGO} ${BAKLOGO}
			echo "原logo已备份为${BAKLOGO}"
			echo $3 $LOGO $SKIP $COUNT
			dd if=${3} of=${LOGO} seek=${SKIP} count=${COUNT} bs=1 conv=notrunc
			if [[ $? -eq 0 ]]; then
				dd if=${LOGO} of=${LOGOPATH} conv=notrunc
				if [[ $? -eq 0 ]]; then
					convert ${3} -gravity center -crop ${W}x${W}+0+0 -resize 100x100 ${3%bmp}png 2>/dev/null
					echo "logo刷写成功"
				else
					echo "logo刷写失败"
				fi
			else
				echo "bmp导入失败"
				cp -f ${BAKLOGO} ${LOGO}
			fi
		else
			echo 'BMP大小与计算有差，需重新导入'
		fi
	elif [[ $2 == regain ]]; then
		dd if=${3} of=${LOGOPATH} conv=notrunc
		if [[ $? -eq 0 ]]; then
			rm -f ${TMP}/*
			echo "恢复成功"
		else
			echo "恢复失败"
		fi
	fi
	exit 0
fi


echo '<?xml version="1.0" encoding="utf-8"?>'

cat <<-EOF
	<group>
		<action reload="true">
			<title>解析logo镜像</title>
			<set>
				source ${PAGE}/firstbootanimation.sh script output
			</set>
		</action>
	</group>
	<group>
		<action reload="true">
			<title>恢复镜像</title>
			<param
				name="INPUTLOGO"
				title="logo镜像"
				type="file"
				suffix="img"
				required="true"/>
			<set>
				source ${PAGE}/firstbootanimation.sh script regain \${INPUTLOGO}
			</set>
		</action>
	</group>
EOF

build()
{
	cat <<-EOF
		<group>
			<action reload="true" icon="${line%bmp}png">
				<title>点击替换图片</title>
				<desc>分辨率:${W}x${H}</desc>
				<param
					name="FILE"
					type="file"/>
				<set>
					source ${PAGE}/firstbootanimation.sh script input $line \${FILE}
				</set>
			</action>
		</group>
	EOF
}

ls ${TMP}/*.bmp | while read line
do
	source ${line%bmp}xxd && build
done

cat <<-EOF
	<group>
		<text>
			<slice>本脚本加了各种判断保证文件大小1b都不差，一般情况下很安全，零误差运行，但不保证一定不会出问题，变砖进售后本人不负责</slice>
		</text>
	</group>
EOF
